package cn.iocoder.yudao.module.erp.controller.admin.recycle;

import cn.hutool.core.collection.CollUtil;
import cn.iocoder.yudao.framework.common.pojo.CommonResult;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.module.erp.controller.admin.recycle.vo.order.ErpRecycleOrderCreateReqVO;
import cn.iocoder.yudao.module.erp.controller.admin.recycle.vo.order.ErpRecycleOrderPageReqVO;
import cn.iocoder.yudao.module.erp.controller.admin.recycle.vo.order.ErpRecycleOrderRespVO;
import cn.iocoder.yudao.module.erp.controller.admin.recycle.vo.order.ErpRecycleOrderUpdateReqVO;
import cn.iocoder.yudao.module.erp.convert.recycle.ErpRecycleOrderConvert;
import cn.iocoder.yudao.module.erp.dal.dataobject.recycle.ErpRecycleOrderDO;
import cn.iocoder.yudao.module.erp.dal.dataobject.recycle.ErpRecycleOrderItemDO;
import cn.iocoder.yudao.module.erp.service.recycle.ErpRecycleOrderService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.annotation.Resource;
import jakarta.validation.Valid;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

import static cn.iocoder.yudao.framework.common.pojo.CommonResult.success;
import static cn.iocoder.yudao.framework.common.util.collection.CollectionUtils.convertList;

@Tag(name = "管理后台 - ERP 回收订单")
@RestController
@RequestMapping("/erp/recycle-order")
@Validated
public class ErpRecycleOrderController {

    @Resource
    private ErpRecycleOrderService recycleOrderService;

    @PostMapping("/create")
    @Operation(summary = "创建回收订单")
    @PreAuthorize("@ss.hasPermission('erp:recycle-order:create')")
    public CommonResult<Long> createRecycleOrder(@Valid @RequestBody ErpRecycleOrderCreateReqVO createReqVO) {
        return success(recycleOrderService.createRecycleOrder(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新回收订单")
    @PreAuthorize("@ss.hasPermission('erp:recycle-order:update')")
    public CommonResult<Boolean> updateRecycleOrder(@Valid @RequestBody ErpRecycleOrderUpdateReqVO updateReqVO) {
        recycleOrderService.updateRecycleOrder(updateReqVO);
        return success(true);
    }

    @PutMapping("/update-status")
    @Operation(summary = "更新回收订单的状态")
    @PreAuthorize("@ss.hasPermission('erp:recycle-order:update')")
    public CommonResult<Boolean> updateRecycleOrderStatus(@RequestParam("id") Long id,
                                                       @RequestParam("status") Integer status) {
        recycleOrderService.updateRecycleOrderStatus(id, status);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除回收订单")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('erp:recycle-order:delete')")
    public CommonResult<Boolean> deleteRecycleOrder(@RequestParam("id") Long id) {
        recycleOrderService.deleteRecycleOrder(id);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得回收订单")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('erp:recycle-order:query')")
    public CommonResult<ErpRecycleOrderRespVO> getRecycleOrder(@RequestParam("id") Long id) {
        ErpRecycleOrderDO recycleOrder = recycleOrderService.getRecycleOrder(id);
        if (recycleOrder == null) {
            return success(null);
        }
        List<ErpRecycleOrderItemDO> recycleOrderItems = recycleOrderService.getRecycleOrderItemListByOrderId(id);
        return success(ErpRecycleOrderConvert.INSTANCE.convert(recycleOrder, recycleOrderItems));
    }

    @GetMapping("/page")
    @Operation(summary = "获得回收订单分页")
    @PreAuthorize("@ss.hasPermission('erp:recycle-order:query')")
    public CommonResult<PageResult<ErpRecycleOrderRespVO>> getRecycleOrderPage(@Valid ErpRecycleOrderPageReqVO pageVO) {
        PageResult<ErpRecycleOrderDO> pageResult = recycleOrderService.getRecycleOrderPage(pageVO);
        if (CollUtil.isEmpty(pageResult.getList())) {
            return success(PageResult.empty(pageResult.getTotal()));
        }
        // 获得项
        List<ErpRecycleOrderItemDO> recycleOrderItems = recycleOrderService.getRecycleOrderItemListByOrderId(
                convertList(pageResult.getList(), ErpRecycleOrderDO::getId));
        return success(ErpRecycleOrderConvert.INSTANCE.convertPage(pageResult, recycleOrderItems));
    }
    
    @PutMapping("/approve")
    @Operation(summary = "审核回收订单")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('erp:recycle-order:update')")
    public CommonResult<Boolean> approveRecycleOrder(@RequestParam("id") Long id) {
        recycleOrderService.approveRecycleOrder(id);
        return success(true);
    }
    
    @PutMapping("/disapprove")
    @Operation(summary = "反审核回收订单")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('erp:recycle-order:update')")
    public CommonResult<Boolean> disapproveRecycleOrder(@RequestParam("id") Long id) {
        recycleOrderService.disapproveRecycleOrder(id);
        return success(true);
    }

} 